#include "form_gzltj.h"
#include "ui_form_gzltj.h"
#include "pub_var.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRecord>

Form_gzltj::Form_gzltj(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_gzltj)
{
    ui->setupUi(this);
    qro = new QSqlQuery(pub::db_o);

    QSettings setIni("clin.ini", QSettings::IniFormat);
    //恢复当前窗口位置
    QRect rct = setIni.value("windowState/gzltj").toRect();
    this->setGeometry(rct);
    //列表字体
    ui->tvItemList->setFont(this->font());
}

Form_gzltj::~Form_gzltj()
{
    delete ui;
}

void Form_gzltj::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/gzltj", QVariant(this->geometry()));

}

void Form_gzltj::on_pbExit_clicked()
{
    this->close();
}

void Form_gzltj::on_pbRun_clicked()
{
    ui->pbRun->setEnabled(false);
    qApp->processEvents();  //刷新界面显示
    ui->leZcgh->setText("0");
    ui->leGhtf->setText("0");
    ui->leZcsf->setText("0");
    ui->leSftf->setText("0");
    ui->leHjje->setText("0");
    ui->leYbzh->setText("0");
    ui->leXjzf->setText("0");
    QString t , sq;
    if(ui->rbWrj->isChecked()){
        t = " and ( balance_flag = '0' or balance_flag is null )";
    }else{
        t = " and oper_date > trunc(sysdate) ";
    }

    sq = "select '挂号', invoice_no, decode ( trans_type,'1', '正', '2', '负' ) , card_no,name,"
         " pact_name,reg_fee+chck_fee+diag_fee+oth_fee,pay_cost,own_cost,valid_flag "
         " from fin_opr_register where oper_code= '" + pub::usr_code + "' " + t +
         "  union"
         "  select  '收费',invoice_no,decode  ( trans_type,'1', '正', '2', '负' ) ,card_no,name,"
         " pact_name,tot_cost,pay_cost,own_cost,cancel_flag "
         "  from fin_opb_invoiceinfo where oper_code= '" + pub::usr_code + "' " + t +
         " order by invoice_no";

    mdList.setQuery(sq, pub::db_o);
    qDebug()<<sq;

    if(mdList.record(0).value(0).toString().isEmpty()){
        QMessageBox::information(this,"注意","未找到指定范围的数据！");
        ui->pbRun->setEnabled(true);
        return;
    }

    mdList.setHeaderData(0, Qt::Horizontal, "类别");
    mdList.setHeaderData(1, Qt::Horizontal, "发票号");
    mdList.setHeaderData(2, Qt::Horizontal, "状态");
    mdList.setHeaderData(3, Qt::Horizontal, "病志号");
    mdList.setHeaderData(4, Qt::Horizontal, "姓名");
    mdList.setHeaderData(5, Qt::Horizontal, "收费方式");
    mdList.setHeaderData(6, Qt::Horizontal, "金额");
    mdList.setHeaderData(7, Qt::Horizontal, "账户支付");
    mdList.setHeaderData(8, Qt::Horizontal, "现金支付");
    mdList.setHeaderData(9, Qt::Horizontal, "有效");
    double width = ui->tvItemList->width() - 50;
    ui->tvItemList->setModel(&mdList);

    ui->tvItemList->setColumnWidth(0, width * 0.06);
    ui->tvItemList->setColumnWidth(1, width * 0.18);
    ui->tvItemList->setColumnWidth(2, width * 0.06);
    ui->tvItemList->setColumnWidth(3, width * 0.12);
    ui->tvItemList->setColumnWidth(4, width * 0.12);
    ui->tvItemList->setColumnWidth(5, width * 0.12);
    ui->tvItemList->setColumnWidth(6, width * 0.1);
    ui->tvItemList->setColumnWidth(7, width * 0.1);
    ui->tvItemList->setColumnWidth(8, width * 0.1);
    ui->tvItemList->setColumnWidth(9, 0);
    ui->tvItemList->setColumnWidth(10, 0);
    ui->tvItemList->setColumnWidth(11, 0);
    ui->tvItemList->setColumnWidth(12, 0);
    ui->tvItemList->setColumnWidth(13, 0);

    sq = "select count(distinct invoice_no) ,valid_flag ,'r' from fin_opr_register "
         " where oper_code= '" + pub::usr_code + "' " + t + "  group by valid_flag, 'r'"
         "  union "
         "select count(distinct invoice_no) ,cancel_flag ,'f' from fin_opb_invoiceinfo "
         " where oper_code= '" + pub::usr_code + "' " + t + " group by cancel_flag, 'r'";
    if(!qro->exec(sq)){
        QMessageBox::warning(this,"注意","计算发票数量出现错误！");
        qDebug()<<qro->lastError().text();
        qDebug()<<qro->lastQuery();
        ui->pbRun->setEnabled(true);
        return;
    }

    while(qro->next()){
        if(qro->value(1).toString() == "1" && qro->value(2).toString() == "r"){
            ui->leZcgh->setText(qro->value(0).toString());
        }
        if(qro->value(1).toString() == "1" && qro->value(2).toString() == "f"){
            ui->leZcsf->setText(qro->value(0).toString());
        }

        if(qro->value(1).toString() == "0" && qro->value(2).toString() == "r"){
            ui->leGhtf->setText(qro->value(0).toString());
        }
        if(qro->value(1).toString() == "0" && qro->value(2).toString() == "f"){
            ui->leSftf->setText(qro->value(0).toString());
        }

    }

    sq = "select sum(reg_fee+chck_fee+diag_fee+oth_fee),sum(pay_cost),sum(own_cost) from fin_opr_register "
         " where oper_code= '" + pub::usr_code + "' " + t +
         "  union "
         "select sum(tot_cost),sum(pay_cost),sum(own_cost) from fin_opb_invoiceinfo "
         " where oper_code= '" + pub::usr_code + "' " + t ;
    if(!qro->exec(sq)){
        QMessageBox::warning(this,"注意","计算金额合计出现错误！");
        qDebug()<<qro->lastError().text();
        qDebug()<<qro->lastQuery();
        ui->pbRun->setEnabled(true);
        return;
    }
    double hj ,zh,xj;
    hj = 0;
    zh = 0;
    xj = 0;

    while(qro->next()){
        hj = hj + qro->value(0).toDouble();
        zh = zh + qro->value(1).toDouble();
        xj = xj + qro->value(2).toDouble();
    }

    ui->leHjje->setText(QString::asprintf("%.2f",hj));
    ui->leYbzh->setText(QString::asprintf("%.2f",zh));
    ui->leXjzf->setText(QString::asprintf("%.2f",xj));

    ui->pbRun->setEnabled(true);
}
